window.test = {};

test.question = [];

test.getCurrentQuestion = function() {
	return gwt.getCurrentQuestion();
};

test.gotoQuestion = function(number) {
	if (gwt.getLastQuestion() + 1 == number) {
		test.submitAll();
		return false;
	}
	var rs = gwt.gotoQuestion(number);
	if (0 != rs) {
		if (rs == 2) {
			alert('テストの順番は固定です。');
		}
		return false;
	} else {

		//update new
		$('#q_no_txt').val(test.getCurrentQuestion());
		return true;
	}
};

test.displayTestInfo = function(title, subtitle, startTime, endTime, type) {

	this.title = title;
	this.subtitle = subtitle;
	this.startTime = parseInt(startTime);
	this.endTime = parseInt(endTime);
	this.type = type;

	var currentTimeMillis = new Date().getTime();

	$(".test_info").find('.title').html(title);
	$(".test_info").find('.subtitle').html(subtitle);
	$(".test_info").find('.start_time').html(new Date(this.startTime).toLocaleString());
	$(".test_info").find('.end_time').html(new Date(this.endTime).toLocaleString());
	if (window.viewScore)
		return;
	//view score

	$('.question_info').append($('.hidden').find('.open_test').clone());

	if (parseInt($('#examinee_id').html()) != 0) {//examinee

		if (test.startTime - currentTimeMillis > 86400000) {// hơn 1 ngày nữa mới thi nên logout luôn
			alert('テストがまだ始まっていません。');
			test.redirect('logout.t02');

		} else if (test.endTime < currentTimeMillis + 500) {// bài thi đã kết thúc rồi => logout luôn
			alert('テストがもう終わりました。');
			test.redirect('logout.t02');
		} else {
			if (currentTimeMillis < test.startTime) {// chờ đến giờ mới được mở bài thi
				$('.question_info').find('.open_test')[0].disabled = true;
				setTimeout(function() {
					$('.question_info').find('.open_test').removeAttr('disabled');
				}, test.startTime - currentTimeMillis);
			}
			setTimeout(function() {//hết giờ thi, tự động logout

				gwt.setSubmit(test.getCurrentQuestion(), true);
				test.submitAll();
				alert("時間です！");
				test.redirect('logout.t02');
			}, test.endTime - currentTimeMillis);
		}
	} else {//simulation

	}

	$('.question_info').find('.open_test').click(function() {
		// var currentTimeMillis = new Date().getTime();
		// if(currentTimeMillis > test.endTime) {
		// alert('The test has finished');
		// return false;
		// }

		test.gotoQuestion(1);
	});

};

test.displayQuestionWR = function(number, content, writes, limitTime) {

	test.storeCurrentQuestion();
	var q = $('.hidden').find('.question_content').clone();
	this.displayQuestion(q, 'QW', number, content, limitTime);
	var answerArea = $(q).find('.answer_area');
	this.addWrites(answerArea, jQuery.parseJSON(writes));

	$('.question_info').html(q);
};

test.displayQuestionS = function(number, content, selections, limitTime) {
	test.storeCurrentQuestion();
	var q = $('.hidden').find('.question_content').clone();
	this.displayQuestion(q, 'QS', number, content, limitTime);
	var answerArea = $(q).find('.answer_area');
	this.addSelections(answerArea, jQuery.parseJSON(selections));

	$('.question_info').html(q);
};

test.displayQuestion = function(q, type, number, content, limitTime) {
	test.question[number] = {};
	test.question[number].type = type;
	if (limitTime > 0) {
		test.question[number].timeout = setTimeout(function() {
			test.question[number].usedTime = limitTime;
			test.gotoQuestion(test.getCurrentQuestion() + 1);
		}, limitTime);
	}
	if (!test.question[number].usedTime)
		test.question[number].usedTime = 0;
	test.question[number].startTime = new Date().getTime();
	$(q).find('.q_no').html(number);
	$(q).find('.content').html(content);

	$(q).find('.submit_question').click(function() {
		test.storeCurrentQuestion();
		test.question[number].submitted = true;
		gwt.setSubmit(number, true);
		test.gotoQuestion(test.getCurrentQuestion() + 1);
	});
};

test.addSelections = function(answerArea, selections) {
	var selectionSample = $('.hidden').find('.selection');
	$.each(selections, function(index, value) {

		var select = $(selectionSample).clone();
		$(select).find('input[type=checkbox]').attr('value', index);
		$(select).find('input[type=checkbox]')[0].checked = value[1];
		//checkbox

		$(select).find('.content').html(value[0]);
		$(answerArea).find('.submit_question').before(select);
	});
};

test.addWrites = function(answerArea, writes) {
	var writeSample = $('.hidden').find('.write');
	$.each(writes, function(index, value) {

		var write = $(writeSample).clone();
		$(write).find('textarea').attr('name', index);
		$(write).find('textarea').attr('maxlength', value[0]);
		$(write).find('textarea').val(value[1]);
		$(answerArea).find('.submit_question').before(write);
	});
};

//store current question
test.storeCurrentQuestion = function() {
	var current = test.getCurrentQuestion();
	if (current != 0) {

		//for fixed test only
		window.clearTimeout(test.question[current].timeout);
		if(test.question[current].usedTime == 0) test.question[current].usedTime = new Date().getTime() - test.question[current].startTime;
		gwt.setUsedTime(current, test.question[current].usedTime);

		//store info
		if (test.question[current].type == 'QS') {
			test.question[current].selected = [];
			$('.question_info').find('.selection').find('input:checked').each(function(index) {
				test.question[current].selected[test.question[current].selected.length] = parseInt($(this).val());
			});
			gwt.storeSelected(current, test.question[current].selected);
		} else {
			test.question[current].write = [];
			$('.question_info').find('.write').find('textarea').each(function(index) {
				test.question[current].write[test.question[current].write.length] = $(this).val();
			});
			gwt.storeWrite(current, test.question[current].write);
		}
	}
};

test.displayScore = function(scoreJSON) {
	$('.left').find('input').hide();
	var score = jQuery.parseJSON(scoreJSON);
	var score1 = -1;
	var score2 = -1;
	$.each(score, function(index, value) {
		if (index == 'score1') {
			score1 = value;
		} else if (index == 'score2') {
			score2 = value;
		}
	});
	$('.main').html('自動採点での得点：' + score1);

};

test.redirect = function(page) {

	var t = window.location.href;
	window.location = t.substring(0, t.lastIndexOf('/') + 1) + page;
};
test.submitAll = function() {
	test.storeCurrentQuestion();
	if (parseInt($('#examinee_id').html()) != 0) {//examinee

		gwt.setSubmit(test.getCurrentQuestion(), true);
		gwt.submitAll();
		//test.redirect('logout.t02');
	} else {
		alert("シミュレーションが終了です。");
		test.redirect('test_manager.t02');
	}
};
function init() {
	$('#q_no_txt').val("0");
	$('#prev_btn').click(function() {
		test.gotoQuestion(test.getCurrentQuestion() - 1);
	});

	$('#next_btn').click(function() {
		if (test.getCurrentQuestion() == 0)
			return;
		// phải mở bài thi đã

		if (gwt.isFixed()) {
			gwt.setSubmit(test.getCurrentQuestion(), true);
			test.question[test.getCurrentQuestion()].submitted = true;
		}
		test.gotoQuestion(test.getCurrentQuestion() + 1);
	});
	$('#q_no_txt').keypress(function(e) {

		if (e.keyCode == 13) {//ENTER key
			test.gotoQuestion(parseInt($(this).val()));
		}
	});
	$('#submit_all').click(function() {// nộp bài đi về :D
		test.submitAll();
	});
	if (window.loadTest) {
		setTimeout(function() {
			gwt.loadTest();
		}, 500);
	} else if (window.viewScore) {
		setTimeout(function() {
			gwt.loadTest();
			gwt.viewScore();
		}, 500);
	}
}
